<?php
	/**
	* Ads_broadcast_model 
	* 
	* Ads_broadcast_model manage the business methods for ads_broadcast table 
	* 
	* 
	* @author Thang
	* @version $Id:  ads_broadcast_model.php 2012-04-13
	* @access public
	*
	*/
	class Ads_broadcast_model extends CI_Model{
		
		public function __construct(){
			$this->load->database();
		}
		
		public function getBroadcastList($id=FALSE){
			$this->db->select('b.*, a.ad_text');
			$this->db->from('ads_broadcast b');
			$this->db->join('ads a', 'a.id = b.ads_id', 'left');
			
			if($id!=FALSE){				
				$this->db->where('b.id = '.$id, NULL, FALSE);
				$query = $this->db->get();	
				return $query->row_array();
			}
			else{
				$query = $this->db->get();	
				return $query->result_array();
			}
		}
		
		public function save(){
			$id = $this->input->post('id');
			
			// get Start date string from input form
			$start_str 	= $this->input->post('start_date') ." ". $this->input->post('start_h').":".$this->input->post('start_i').":".$this->input->post('start_s');
			
			// get End date string from input form
			$end_str 	= $this->input->post('end_date') ." ". $this->input->post('end_h').":".$this->input->post('end_i').":".$this->input->post('end_s');
			
			// convert start date and end date string to sql format
			$start_date = date('Y-m-d h:i:s', strtotime($start_str));
			$end_date 	= date('Y-m-d h:i:s', strtotime($end_str));
			
			$data = array(
				"ads_id" 		=> $this->input->post('ads_id'),
				"status"  		=> $this->input->post('status'),
				"start_date"	=> $start_date,
				"end_date"		=> $end_date
			);
			if(empty($id))
				return $this->db->insert('ads_broadcast', $data);
			else{
				$this->db->where('id', $id);
				return $this->db->update('ads_broadcast', $data);
			}
		}		
		
		public function delete($id){
			$this->db->where('id', $id);
			$this->db->delete('ads_broadcast');
		}
	}
?>